package cn.good.yan.controller;

import cn.hutool.core.util.IdUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 演示：Resilience4j  feign的远程调用
 *
 * @author shengren.yan
 * @create 2022-12-04
 */
@RestController
public class PayCircuitController {

    @GetMapping(value = "/rescire1")
    public String rescire1() {
        return "请求成功, id:  " + " \t " + IdUtil.simpleUUID();
    }

    //=========Resilience4j CircuitBreaker 的例子：演示：服务降级和熔断的
    @GetMapping(value = "/rescire/{id}")
    public String myCircuit(@PathVariable("id") Integer id) {
        if (id == -4) {
            throw new RuntimeException("---- id 不能负数");
        }
        if (id == 9999) {
            try {
                // 模拟请求超时
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return "请求成功, id:  " + id + " \t " + IdUtil.simpleUUID();
    }

    //=========Resilience4j CircuitBreaker 的例子：演示：依赖隔离 - 用来限制最大并发数量的限制 （限制最大并发）
    @GetMapping(value = "/rescire2/{id}")
    public String myCircuit2(@PathVariable("id") Integer id) {
        if (id == -4) {
            throw new RuntimeException("---- id 不能负数");
        }
        if (id == 9999) {
            try {
                // 模拟请求超时
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return "请求成功 bulkhead, id:  " + id + " \t " + IdUtil.simpleUUID();
    }

    //=========Resilience4j ratelimit 的例子 演示：限流
    @GetMapping(value = "/rescire3/{id}")
    public String rescire3(@PathVariable("id") Integer id) {
        return "Hello, 限流的---欢迎到来 inputId:  " + id + " \t " + IdUtil.simpleUUID();
    }

}
